#/usr/bin/python

import re

class Util:
    def javaid_to_sqlid(self,id):
        ids=re.findall(r'[A-Z][a-z]+',id)
        sql_id=None
        if ids.__len__()==0:
            print("warning:make sure that you have the define like upperCase lkie UserName or User"+id)
            return id
        for id in ids:
            if sql_id==None:
                sql_id=id.lower()
            else:
                sql_id=sql_id+"_"+ id.lower()
        return sql_id

    def sqlid_to_javaid(self,id):
        ids=id.split("_")
        java_id=None
        if(ids.__len__()==0):
            return id[0].upper+id[1:]
        for localid in ids:
            if java_id==None:
                java_id=localid
            else:
                java_id=java_id+localid[0].upper()+localid[1:]
        return java_id

    def javatype_to_sqltype(self,javatype):
        javatype=re.sub("\s","",javatype)
        typetable={
                'Int':'int',
                'int':'int',
                'Long':'bigint',
                'long':'bigint',
                'String':'varchar(255)',
                'Timestamp':'timestamp',
                }
        sqltype=typetable.get(javatype)
        if sqltype==None:
            print("java type to sqltype error type!please report bug or just check you type:"+javatype)
        return sqltype
    def domain_to_ResultMap(self,domain):
        domain=domain[0].lower()+domain[1:]+"Mapper"
        return domain
    def domain_to_resultTitle(self,domain):
        map_str=domain[0].lower()+domain[1:]+"-mapper.xml"
        return map_str
    def domain_to_firstlowercase(self,domain):
        map_str=domain[0].lower()+domain[1:]
        return map_str



class ItemTable:
    def __init__(self):
        #table:java id,java type
        self.table=[]
    def add_list(self,typid,javatype):
        typeid=javatype[0].upper()+javatype[1:]
        id_colum=(javaid,javatype,typeid)
        self.table.append(id_colum)

class InterfaceTable:
    def __init__(self):
        self.parameter=[]
        self.returntype=None
    def set_id(self,id):
        self.id=id
    def add_parameter(self,idtype,id):
        self.parameter.append((idtype,id))
    def set_return(self,idtype):
        self.returntype=idtype
    def gen_interface(self):
        gen_dao_str="public "+self.returntype+" "+self.id+"("
        parameter_str=None
        for parameter in self.parameter:
            if parameter_str==None:
                parameter_str=parameter[0]+" "+parameter[1]
            else:
                parameter_str=parameter_str+","+parameter[0]+" "+parameter[1]
        gen_dao_str=gen_dao_str+parameter_str+");"
        return gen_dao_str


    def gen_method(self):
        gen_dao_str="public "+self.returntype+" "+self.id+"("
        parameter_str=None
        for parameter in self.parameter:
            if parameter_str==None:
                parameter_str=parameter[0]+" "+parameter[1]
            else:
                parameter_str=parameter_str+","+parameter[0]+" "+parameter[1]
        gen_dao_str=gen_dao_str+parameter_str+"){return null;}"
        return gen_dao_str



class InterfaceParser:
    def __init__(self,domain):
        self.domain=domain
        self.interface_table=[]
    def parser(self,sentences):
        util=Util()
        sentences=re.sub("^\s+","",sentences)
        sentence=sentences.split("|")
        for single_sentence in sentence:
            self.interface_table.append(self.parse_single_interface(single_sentence))
    def parse_single_interface(self,sentences):
        single_inter=InterfaceTable()
        for sentence in sentences.split(","):
            typeid=sentence.split(":")[0]
            if typeid == 'id':
                single_inter.set_id(sentence.split(":")[1])
            elif typeid == 'parameter':
                #pdb.set_trace()
                ids=re.split("[ ]",sentence.split(":")[1])
                single_inter.add_parameter(ids[0],ids[1])
            elif typeid == 'return':
                single_inter.set_return(sentence.split(":")[1])
        return single_inter
    def get_interface_table(self,sentence):
        self.parser(sentence)
        return self.interface_table


        
if __name__ == "__main__":
    mParser=Util()    
    print(mParser.javaid_to_sqlid("sid"))
    print(mParser.sqlid_to_javaid("Sid"))
    #table=InterfaceTable()
    #table.add_parameter("fuck","time")
    #table.set_id("helloworld")
    #table.set_return("hello")
    #print(table.gen_method())
    #print(table.gen_interface())


